<?php
/**
 * Created by PhpStorm.
 * User: yancheng<cheng@love.xiaoyan.me>
 * Date: 2019/3/2
 * Time: 1:31 PM
 */
include 'inc/env.php';
if (!isset($_POST['login_user']) || empty($_POST['login_user'])) {
    //location.href='login.php?error=1';
    die("<script>alert('用户名格式不正确');history.back()</script>");
}
if (!isset($_POST['login_pass']) || empty($_POST['login_pass'])) {
    die("<script>alert('密码格式不正确');history.back()</script>");
}
if (!isset($_POST['login_pass2']) || empty($_POST['login_pass2'])) {
    die("<script>alert('密码格式不正确');history.back()</script>");
}
//获取提交数据
$username = $_POST['login_user'];
$password = $_POST['login_pass'];
$password2 = $_POST['login_pass2'];
if ($password != $password2) {
    die("<script>alert('两次输入的密码不相等');history.back()</script>");
}

$all_user_data = get_all_users();
if (isset($all_user_data[$username])) { //  判断是否存在用户
    die("<script>alert('登录用户已经存在');history.back()</script>");
}
//保存注册用户数据
$result = create_user_data($username, md5($password));
if ($result) {
    die("<script>alert('注册成功请登录');location.href='login.php?from=reg';</script>");
} else {
    die("<script>alert('注册失败请重试');history.back()</script>");
}

